<?php

	namespace Phptricks\system\library\view;

	use Phptricks\system\core\library\library;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\library\loaders\loader as loaders;
	use Phptricks\system\library\custom_fields\custom_fields as custom_fields;

	class view implements library{

		private static $_instance = null;
		private $_load = null;
		private $_config = null;
		public $_smarty = null;
		public $custom_fields = null;

		public function __construct($config){
			$this->custom_fields = custom_fields::getinstance();
			$this->_load = loaders::getinstance();

			$this->_initialize($config);
			return true;
		}

		public static function getinstance($config = ''){
			if (!isset($config) || empty($config)) {
				throw new Exc("You must enter a config data in param \$config");
			}
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\system\library\view\view($config);
			}
			return self::$_instance;
		}

		public function _initialize($config){
			$this->_config = $config;
			$this->start_template_engine();
			return true;
		}

		public function start_template_engine(){
			switch (\strtolower($this->_config->template_engine)) {
				case 'smarty':
					$this->_load->_Library('Smarty.class');
					$this->_smarty = new \Smarty();
					$this->_smarty->template_dir = \APPPATH . $this->_config->template_dir;
					if (!empty($this->_load->_registerView) && count($this->_load->_registerView) > 0) {
						$this->_smarty->addTemplateDir($this->_load->_registerView);
					}
					$this->_smarty->compile_dir = \APPPATH . $this->_config->template_cach;
					$this->_smarty->config_dir = \APPPATH . $this->_config->template_config_dir;
					$this->_smarty->cache_dir = \APPPATH . $this->_config->template_cache_dir;
					$this->_smarty->assign('view', (array) $this->_config);
					break;
			}
			return true;
		}

	}
	